package xyz.chaobei.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import xyz.chaobei.redis.model.User;
import xyz.chaobei.redis.repository.RedisRepository;

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    private RedisRepository<User, Integer> redisRepository;

    @Override
    public void run(String... args) throws Exception {

        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setUsername("mrc:" + i);
            user.setPassword("123456");

            redisRepository.save(user);
        }

        User user = redisRepository.findById(0);
        System.out.println("User 0 =" + user);


        boolean isDel = redisRepository.deleteById(0);
        System.out.println("Del 0 =" + isDel);

        user.setUsername("update");
        user.setPassword("update");

        redisRepository.update(user);

        User updateUser = redisRepository.findById(0);
        System.out.println("updateUser 0 =" + updateUser);
    }
}
